define(function () {
    'use strict';
    function ctrl($scope, $stateParams,$ionicLoading, LocalCacheService, CommonService, WebService) {
        function init() {
            $scope.account = {
                accountType: $stateParams.typeid,
                checked: false
            };
            $scope.openTA = {};
            $scope.user = LocalCacheService.getUser();
            //根据开户类型获取TA开户申请状态
            var params = {client_id:$scope.user.client_id,fund_account:$scope.user.fund_account};
            $ionicLoading.show();
            WebService.qryOfStockHolderAccount(params).then(function (data) {
                $ionicLoading.hide();
                var accountType=$scope.account.accountType;
                var stock_account=null;
                var fund_company=null;
                if(accountType=='1'){
                    $scope.openTA.status=data.H_TA.open_status;
                    stock_account=data.H_TA.stock_account;
                    fund_company='99';
                }else  if(accountType=='2'){
                    $scope.openTA.status=data.S_TA.open_status;
                    stock_account=data.S_TA.stock_account;
                    fund_company='98';
                }
                if($scope.openTA.status=='1'){
                    if(fund_company =='99' ){
                        $scope.openTA.subtext="您已有上海TA（场外基金）账户（" + stock_account + "）。如有疑问，请拨打客服热线：400-800-5000。";
                    }else if(fund_company =='98' ){
                        $scope.openTA.subtext="您已有深圳TA（场外基金）账户（" + stock_account + "）。如有疑问，请拨打客服热线：400-800-5000。";
                    }
                } else if($scope.openTA.status=='9'){
                    $scope.openTA.subtext="您已经提交了开户申请，系统正在处理中，无需再次提交申请。如有疑问，请拨打客服热线：400-800-5000。";
                }
            },function(){
                $ionicLoading.hide();
                $scope.openTA.status='5';
                $scope.openTA.subtext="获取信息错误";
            });

        }
        $scope.$on('$ionicView.beforeEnter', function () {
            init()
        });
        $scope.submit = function () {
           //提交TA开户
            var accountType=$scope.account.accountType;
            var fund_company=null;
            if(accountType=='1'){//沪A
                fund_company='99';
            }else  if(accountType=='2'){//深A
                fund_company='98';
            }
            var params = {client_id:$scope.user.client_id, fund_account: $scope.user.fund_account,fund_company:fund_company };
            $ionicLoading.show();
            WebService.addOfStockHolderAccount(params).then(function (data) {
                $ionicLoading.hide();
                if(data.error_info){
                    $scope.openTA.status = 1;
                    $scope.openTA.subtext=data.error_info;
                }else{
                    CommonService.showComplete('../view/template/accounttemplate.html', '返回首页', 'tab.account-index');
                }
            },function(){
                $ionicLoading.hide();
                CommonService.showAlert({message:"提交失败"});
            });
        }
    }

    ctrl.$inject = ['$scope', '$stateParams','$ionicLoading', 'LocalCacheService', 'CommonService', 'WebService'];
    return ctrl;
});